//
//  NewFeatureCollectionViewController.swift
//  Weibo T
//
//  Created by mac on 15/11/30.
//  Copyright © 2015年 陈肖坤. All rights reserved.
//

import UIKit
import SnapKit

private let reuseIdentifier = "Cell"
private let maxImageCount = 4

class NewFeatureCollectionViewController: UICollectionViewController {

    init() {
        let layoder = UICollectionViewFlowLayout()
        //设置大小
        layoder.itemSize = UIScreen.mainScreen().bounds.size
        //滚动方向
        layoder.scrollDirection = .Horizontal
        layoder.minimumInteritemSpacing = 0
        layoder.minimumLineSpacing = 0
        
        //调用super 
        super.init(collectionViewLayout: layoder)
        //设置分页
        collectionView?.pagingEnabled = true
        //设置弹簧效应
        collectionView?.bounces = false
        //显示水平滚动指标
        collectionView?.showsHorizontalScrollIndicator = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        

        // Register cell classes
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

   
    // MARK: UICollectionViewDataSource

        override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return maxImageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
        cell.index = indexPath.item
//        cell.backgroundColor = indexPath.item %  2 == 0 ? UIColor.redColor() : UIColor.blueColor()
        return cell
    }
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        print(indexPath.item)
        let cell = collectionView.visibleCells().last as! NewFeatureCell
        let index = collectionView.indexPathForCell(cell)
        let indexItem = index?.item ?? 0
        if indexItem == maxImageCount - 1 {
            cell.startAnimation()
        }
    }
}

//MARK: 自定义cell
class NewFeatureCell: UICollectionViewCell {
    //定义index属性
    var index: Int = 0 {
        didSet {
            iconView.image = UIImage(named: "new_feature_\(index + 1)")
            startBtn.hidden = true
        }
    }
    //重写初始化方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    //设置试图布局
    private func setupUI() {
    
        //添加子控件
        contentView.addSubview(iconView)
        contentView.addSubview(startBtn)
        
        //设置布局
        iconView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
        startBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView.snp_centerX)
            //添加底部约束
            make.bottom.equalTo(contentView.snp_bottom).offset(-200)
        }
        //添加点击事件
        startBtn.addTarget(self , action: "startBtnDidClick", forControlEvents: .TouchUpInside)
    }
    //添加动画效果
    func startAnimation() {
        //动画开始之前隐藏 按钮
        startBtn.hidden = false
        //设置初始化形变
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        /**
        iOS 7.0 推出的动画效果
        - duartion                          持续时间
        - parameter delay:                  延迟时间
        - parameter usingSpringWithDamping: 弹簧效果 0.1 ~ 1之间  越小越弹
        - parameter initialSpringVelocity:  加速度
        - parameter options:                动画可选项
        - parameter animations:             完成动画闭包
        - parameter completion:             动画结束后的闭包
        */
        UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            self.startBtn.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                print("ok")
        }
    
    }
    //点击事件
    @objc private func startBtnDidClick() {
        print(__FUNCTION__)
        //发出通知页面切换
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVCNotification, object: nil)        
    }
    
        //懒加载子试图
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    //懒加载立即体检按钮
    private lazy var startBtn: UIButton = {
    let btn = UIButton()
    btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
    btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
    btn.setTitle("立即体检", forState: .Normal)
        
        btn.sizeToFit()
        return btn
    }()
}